<?php
namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\web\Response;
use app\models\LoginForm;
use app\models\ContactForm;

class BrandController extends AuthController
{
    public $currentHeader = 'header-operation';
    public $currentNav = 'brand-nav';
    public $pageTitle = '';

    public function init() {
        parent::init();
        $this->operators = $this->service->setRef();
        $service = new \app\services\CreativeService();
        $adnetworkId = $this->S('ad_network_id');
        $this->categories = $service->getAllCategories($adnetworkId); 
    }

    public function actionBrandList() {
        $this->pageTitle = $this->t('advertiser', 'Brand List');
        return $this->show();
    }

    public function actionNewBrand() {
        $this->pageTitle = $this->t('advertiser', 'Add Brand');
        return $this->show();
    }

    public function actionBrandEdit () {
        $this->pageTitle = $this->t('advertiser', 'Edit Brand');
        $advertiserId = $this->I('id');
        $advertiser = $this->M('Advertiser', $advertiserId);
        $this->advertiser = $advertiser;
        $service = new \app\services\CreativeService();
        $category = $service->getCategoryNameById($advertiser['category_id']);
        $this->category = $category;
        $this->bindUsers = $this->getAll('UserResource', ['resource_type' => 2, 'resource_id' => $advertiserId, 'status' => 1]);
        return $this->show();
    }

    public function actionNewBrandHandle() {
        $post = $this->I();
        list($status, $data) = $this->service->addNewAdvertiser($post);
        if ($status) {
            $this->OK($this->t('advertiser', 'Add advertiser successfully!'));
        }
        $this->NG($this->t('advertiser', 'Add advertiser failed!'));
    }

    public function actionBrandEditHandle() {
        $post = $this->I();
        list($status, $data) = $this->service->addNewAdvertiser($post);
        if ($status) {
            $this->OK($this->t('advertiser', 'Update advertiser successfully!'));
        }
        $this->NG($this->t('advertiser', 'Update advertiser failed!'));
    }

    public function actionGetBrands() {
        $advertisers = $this->service->getAllAdvertisers(); 
        $this->jsonReturn(['data' => $advertisers]);
    }
}// end of advertiser controller declaration
